package com.hr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hr.entity.*;
import com.hr.service.*;
import com.hr.utils.R;
import com.hr.utils.ResumeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2021-10-15
 */
@CrossOrigin
@RestController
@RequestMapping("/hr/service/resume")
public class ResumeController {

    @Autowired
    BaseInfoService baseInfoService;

    @Autowired
    JobService jobService;

    @Autowired
    RegisterService registerService;

    @Autowired
    EduInfoService eduInfoService;

    @Autowired
    PersonalInfoService personalInfoService;

    public Resume getResume(Serializable userId) {
        Resume resume = new Resume();
        QueryWrapper<BaseInfo> baseInfoWrapper = new QueryWrapper<>();
        baseInfoWrapper.eq("user_id", userId);
        BaseInfo baseInfo = baseInfoService.getOne(baseInfoWrapper);
        baseInfo.setJobName(jobService.getById(baseInfo.getJob()).getJobName());
        resume.setBaseInfo(baseInfo);

        QueryWrapper<EduInfo> eduInfoWrapper = new QueryWrapper<>();
        eduInfoWrapper.eq("user_id", userId);
        List<EduInfo> eduInfos = eduInfoService.list(eduInfoWrapper);
        if(eduInfos.size() > 0) {
            resume.setEduInfos(eduInfos);
        } else {
            resume.setEduInfos(null);
        }

        QueryWrapper<PersonalInfo> personalInfoWrapper = new QueryWrapper<>();
        personalInfoWrapper.eq("user_id", userId);
        PersonalInfo personalInfo = personalInfoService.getOne(personalInfoWrapper);
        resume.setPersonalInfo(personalInfo);

//        System.out.println("----------------------------------------------------------------");
//        System.out.println("----------------------------------------------------------------");
//        System.out.println("----------------------------------------------------------------");
//        System.out.println(resume.getEduInfos());
//        System.out.println("----------------------------------------------------------------");
//        System.out.println("----------------------------------------------------------------");
//        System.out.println("----------------------------------------------------------------");

        return resume;
    }

    @GetMapping("print")
    public R print_resume(String userId) {
        ResumeUtils resumeUtils = new ResumeUtils();
        Resume resume = getResume(userId);
        boolean autoCode = resumeUtils.createAutoCode(resume);
        return autoCode ? R.ok() : R.error();
    }

    @GetMapping("printAll/{jobId}")
    public R print_resume(@PathVariable("jobId") Integer jobId) {
        ResumeUtils resumeUtils = new ResumeUtils();
        List<Integer> ids = new ArrayList<>();

        QueryWrapper<Register> registerWrapper = new QueryWrapper();
        registerWrapper.eq("job_id", jobId);
        List<Register> registers = registerService.list(registerWrapper);

        for(Register register: registers) {
            ids.add(register.getUserId());
        }

        for(Integer id: ids) {
            Resume resume = getResume(id);
            boolean autoCode = resumeUtils.createAutoCode(resume);
        }
        return R.ok();
    }
}
